package com.example.bozai.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应结果封装
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功状态码
     */
    public static final int SUCCESS = 200;

    /**
     * 错误状态码
     */
    public static final int ERROR = 500;

    private int code;
    private String msg;
    private T data;

    public Result() {
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功响应
     */
    public static <T> Result<T> success() {
        return new Result<>(SUCCESS, "操作成功", null);
    }

    /**
     * 成功响应带数据
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS, "操作成功", data);
    }

    /**
     * 错误响应
     */
    public static <T> Result<T> error() {
        return new Result<>(ERROR, "操作失败", null);
    }

    /**
     * 错误响应带消息
     */
    public static <T> Result<T> error(String msg) {
        return new Result<>(ERROR, msg, null);
    }

    /**
     * 自定义响应
     */
    public static <T> Result<T> build(int code, String msg, T data) {
        return new Result<>(code, msg, data);
    }
}
